package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.redis.GiftWallRedisDao;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.Gift;
import com.juyu.miliao.api.domain.vo.GiftWallLikeInfoVO;
import com.juyu.miliao.api.domain.vo.GiftWallVo;
import com.juyu.miliao.api.util.PageUtil;
import com.juyu.miliao.common.constant.GiftWallRedisKey;
import com.juyu.miliao.common.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

/**
 * 礼物墙
 *
 * @author xuanqicheng
 * @create 2019-18-14:28
 */
@Service
@Slf4j
public class GiftWallService {
    public static final int IS_TOP_NUMBER = 1500;
    @Autowired
    private GiftWallRedisDao giftWallRedisDao;

    @Autowired
    private UserInfoCache userInfoCache;

    @Autowired
    private GiftService giftService;


    public Map<String, Object> getOldPageInfo(long userId, Integer page, Integer pageSize) {
        Map<String, Object> map = new HashMap<>(2);
        int startNum = PageUtil.getOffset(page, pageSize);

        Set<String> maxSet = giftWallRedisDao.getGiftByMax();

        if (maxSet != null && maxSet.size() > 0) {

            List<String> maxList = new ArrayList<>(maxSet);

            GiftWallVo maxGiftWallVo = JSONObject.parseObject(maxList.get(maxList.size() - 1), GiftWallVo.class);

            setGiftWallVo(userId,maxGiftWallVo);

//            boolean bl = giftWallRedisDao.checkLikeInfo(userId, maxGiftWallVo.getSpendId());
//            if (bl) {
//                maxGiftWallVo.setIsLike(1);
//            }
//
//            GiftWallLikeInfoVO maxGiftWallLikeInfoVO = setGiftWallLikeInfo(maxGiftWallVo.getSpendId());
//
//            maxGiftWallVo.setLikeInfo(maxGiftWallLikeInfoVO);


            map.put("top", maxGiftWallVo);

            String key = GiftWallRedisKey.GIFT_WALL_ZSET + LocalDate.now().toString();

            Set<String> giftWallSet = giftWallRedisDao.getGiftInfo(key, startNum, (startNum + pageSize) - 1);

            List<GiftWallVo> giftWallVoList = new ArrayList<>();
            if (giftWallSet != null && giftWallSet.size() > 0) {
                List<String> list = new ArrayList<>(giftWallSet);
                for (String str : list) {

                    GiftWallVo giftWallVo = JSONObject.parseObject(str, GiftWallVo.class);

                    setGiftWallVo(userId,giftWallVo);


                    giftWallVoList.add(giftWallVo);
                }
            }

            map.put("giftWallList", giftWallVoList);
        }


        return map;
    }

    private GiftWallLikeInfoVO setGiftWallLikeInfo(int sendId) {
        GiftWallLikeInfoVO giftWallLikeInfoVO = new GiftWallLikeInfoVO();

        long likeSize = giftWallRedisDao.getLikeSizeInfo(sendId);

        giftWallLikeInfoVO.setSize(likeSize);

        if (likeSize > 0) {

            Set<String> likeSet = giftWallRedisDao.getLikeInfo(sendId);

            List<String> iconList = new ArrayList<>();

            for (String str : likeSet) {
                UserCacheVo userCacheVo = userInfoCache.getUserCacheInfo(Long.parseLong(str));
                iconList.add(userCacheVo.getIcon());
            }

            giftWallLikeInfoVO.setIconList(iconList);
        }
        return giftWallLikeInfoVO;
    }


    /**
     *  设置礼物墙信息
     * @param userId 当前访问用户
     * @param giftWallVo
     */
    private void setGiftWallVo(long userId,GiftWallVo giftWallVo) {
        UserCacheVo inUserCache = userInfoCache.getUserCacheInfo(giftWallVo.getInUserId());

        UserCacheVo outUserCache = userInfoCache.getUserCacheInfo(giftWallVo.getOutUserId());

        Gift gift = giftService.getGiftByIdNoCache(giftWallVo.getGiftId());
        String showTime = DateUtil.getShowTime(giftWallVo.getCreateTime());

        giftWallVo.setInUserNickName(inUserCache.getNickName())
                .setInUserIcon(inUserCache.getIcon())
                .setInUserAge(inUserCache.getAge())
                .setInUserSex(inUserCache.getSex())
                .setInUserMGrade(inUserCache.getMGrade())
                .setInUserVGrade(inUserCache.getVGrade())
                .setInUserIsVip(inUserCache.getIsVip())
                .setInVipLevel(inUserCache.getVipLevel())
                .setInStar(inUserCache.getStar())
                .setInUserIsHost(inUserCache.getIsHost())
                .setOutUserNickName(outUserCache.getNickName())
                .setOutUserAge(outUserCache.getAge())
                .setOutUserIcon(outUserCache.getIcon())
                .setOutUserMGrade(outUserCache.getMGrade())
                .setOutUserVGrade(outUserCache.getVGrade())
                .setOutUserSex(outUserCache.getSex())
                .setOutUserIsVip(outUserCache.getIsVip())
                .setOutVipLevel(outUserCache.getVipLevel())
                .setOutStar(outUserCache.getStar())
                .setOutUserIsHost(outUserCache.getIsHost())
                .setGiftName(gift.getGiftName())
                .setGiftIcon(gift.getIcon())
                .setShowTime(showTime);

        GiftWallLikeInfoVO giftWallLikeInfoVO = setGiftWallLikeInfo(giftWallVo.getSpendId());

        boolean isGiftLike = giftWallRedisDao.checkLikeInfo(userId, giftWallVo.getSpendId());
        if (isGiftLike) {
            giftWallVo.setIsLike(1);
        }

        giftWallVo.setLikeInfo(giftWallLikeInfoVO);

    }

    public Object setLike(long userId, int sendId) {
        boolean bl = giftWallRedisDao.checkLikeInfo(userId, sendId);
        if (bl) {
            Map<String, Object> map = new HashMap<>(2);
            giftWallRedisDao.removeLikeInfo(userId, sendId);
            map.put("type", "remove");
            return map;
        } else {
            Map<String, Object> map = new HashMap<>(2);
            giftWallRedisDao.setLikeInfo(userId, sendId);
            map.put("type", "add");
            return map;
        }
    }

    public GiftWallLikeInfoVO getGiftWallLikeInfo(Integer sendId) {
        GiftWallLikeInfoVO vo = setGiftWallLikeInfo(sendId);
        return vo;
    }


    /**
     * 判断是不是小于指定版本
     *
     * @param targetVersion
     * @param versions
     * @return
     */
    public boolean checkOldVersion(String[] targetVersion, String[] versions) {
        boolean bl = false;
        for (int i = 0; i < versions.length; i++) {
            int v = Integer.parseInt(versions[i]);
            if (i > targetVersion.length - 1) {
                bl = true;
            } else {
                int l = Integer.parseInt(targetVersion[i]);
                if (v > l) {
                    bl = true;
                }
            }

        }

        return bl;
    }


    /**
     * 获取礼物列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    public Object getPageInfo(long userId, Integer page, Integer pageSize) {

        int startNum = PageUtil.getOffset(page, pageSize);

        LocalDate localDate = LocalDate.now();
        //当前日期是第几周
        int weeksByYear = DateUtil.getWeeksByYear(1, localDate);

        String key = GiftWallRedisKey.GIFT_WALL_NEW_LIST_ZSET + localDate.getYear() + ":" + weeksByYear;
        log.info("获取礼物墙KEY="+key);
        Set<String> giftWallSet = giftWallRedisDao.getGiftInfo(key, startNum, (startNum + pageSize) - 1);
        log.info("礼物墙大小："+giftWallSet.size());
        List<GiftWallVo> giftWallVoList = new ArrayList<>();
        if (giftWallSet != null && giftWallSet.size() > 0) {

            List<String> list = new ArrayList<>(giftWallSet);

            for (String str : list) {

                GiftWallVo giftWallVo = JSONObject.parseObject(str, GiftWallVo.class);

                setGiftWallVo(userId,giftWallVo);
                if(giftWallVo.getSpend()>=IS_TOP_NUMBER){
                    giftWallVo.setIsTop(1);
                }
                giftWallVoList.add(giftWallVo);
            }

            return giftWallVoList;
        } else {
            return giftWallVoList;
        }

    }

}
